#pragma once
#include "VirtualP2PServer.hh"
#include "mythVirtualDecoder.hh"
#include <string>
#include <stdio.h>
class mythP2PDecoder :
	public mythVirtualDecoder
{
public:
	static mythP2PDecoder* CreateNew(int userid,const char* filename){
		return new mythP2PDecoder(userid,filename);
	}
	static std::string onLinkStatic(void* data){
		mythP2PDecoder* decoder = (mythP2PDecoder*) data;
		return decoder->onLink();
	}
	static void OnConnectionStatic(std::string ip, int port, std::string rtmplink, int UdpSock, void* data){
		mythP2PDecoder* decoder = (mythP2PDecoder*) data;
		return decoder->OnConnection(ip, port, rtmplink, UdpSock);
	}
	static void OnRecvStatic(void* src, int len, void* data){
		mythP2PDecoder* decoder = (mythP2PDecoder*) data;
		return decoder->OnRecv(src, len);
	}
	int MainLoop();
	~mythP2PDecoder();
protected:
	mythP2PDecoder(int userid,const char* filename);
	void OnRecv(void* src, int len);
	void OnConnection(std::string ip, int port, std::string rtmplink, int UdpSock);
	std::string onLink();
private:
	std::string _filename;
	VirtualP2PServer* _server;
	int _userid;
	char* _buffer;
	int _bufferindex;
};
